<?php
class User {
	//Ä�Äƒng kiÌ� nhaÌ€ Ä‘Ã¢Ì€u tÆ°
	public function setUser($name, $clas, $id)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$name = utf8_encode($name);
		$clas =  utf8_encode(mysql_real_escape_string($clas));
		$id =  mysql_real_escape_string($id);
		$sql = "INSERT INTO user VALUES (NULL, '$name', '$clas', '$id', 15000)";
		echo $sql;
		//echo utf8_encode("quÃƒÂ¢n");
		if(!($result = mysql_query($sql)))
		die(mysql_error());
	}
	//Show baÌ‰ng xÃªÌ�p haÌ£ng nhaÌ€ Ä‘Ã¢Ì€u tÆ°
	public function showRank($number)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "SELECT * FROM user INNER JOIN usermoney ON (user.userid = usermoney.userid) ORDER BY usermoney.money DESC";
		if(!($result = mysql_query($sql))) die(mysql_error());
		echo "<table border='1'>";
		echo "<tr><th>Name</th>
				<th>Class</th>
				<th>Money</th>
				</tr>";
		$cNumber = 0;
		while($row = mysql_fetch_array($result))
		{
			echo "<tr>";
			echo "<td>" . $row['Name'] . "</td>";
			echo "<td>" . $row['Class'] . "</td>";
			echo "<td>" . $row['Money'] . "</td>";
			echo "</tr>";
			$cNumber++;
			if ($cNumber == $number)
			{
				break;
			}
		}
		echo "</table>";
	}
	//Show danh saÌ�ch nhaÌ€ Ä‘Ã¢Ì€u tÆ°
	public function showUser()
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "SELECT * FROM user INNER JOIN usermoney ON (user.userid = usermoney.userid)";
		if(!($result = mysql_query($sql))) die(mysql_error());
		echo "<table border='1'>";
		echo "<tr><th>Name</th>
				<th>Class</th>
				<th>Money</th>
				<th> </th>
                                <th> </th>
				</tr>";
		while($row = mysql_fetch_array($result))
		{
			echo "<tr>";
			echo "<td>" . $row['Name'] . "</td>";
			echo "<td>" . $row['Class'] . "</td>";
			echo "<td>" . $row['Money'] . "</td>";
			echo "<td><button onclick=deleteUser('".$row['UserID']."')>Delete</button></td>";
			echo "<td><button onclick=showEditUser('".$row['UserID']."')>Edit</button></td>";
			echo "</tr>";
		}
		echo "</table>";
	}
	//XoÌ�a mÃ´Ì£t nhaÌ€ Ä‘Ã¢Ì€u tÆ° trong danh saÌ�ch
	public function deleteUser($userid)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "DELETE FROM user WHERE (UserID='$userid')";
		if(!($result = mysql_query($sql))) die(mysql_error());
	}

	public function editUser($userid,$name,$clas,$id)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$name=  utf8_encode($name);
		$sql = "UPDATE user SET Name='$name', Class='$clas', StudentID='$id' WHERE (UserID='$userid')";
		if(!($result = mysql_query($sql))) die(mysql_error());

	}
	//Show phÃ¢Ì€n chiÌ‰nh sÆ°Ì‰a thÃ´ng tin nhaÌ€ Ä‘Ã¢Ì€u tÆ°
	public function showEditUser($userid)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "SELECT Name,Class,StudentID,Money FROM user WHERE (UserID='$userid')";
		if(!($result = mysql_query($sql))) die(mysql_error());
		mysql_close();
		echo "<table border='1'>";
		echo "<tr><th>Name</th>
				<th>Class</th>
				<th>StudentID</th>
				<th>Money</th>
				</tr>";
		while($row = mysql_fetch_array($result))
		{
			echo "<tr>";
			echo "<td><input type='text' name='ename' id='ename' maxlength='50' value='" . $row['Name'] . "'/></td>";
			echo "<td><input type='text' name='eclass' id='eclass' maxlength='50' value='" . $row['Class'] . "'/></td>";
			echo "<td><input type='text' name='eid' id='eid' maxlength='50' value='" . $row['StudentID'] . "'/></td>";
			echo "<td>" . $row['Money'] . "</td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td colspan='2'><button id='confirmEdit' onclick=editUser('$userid') disabled>Confirm Edit</button></td>";
			echo "</tr";
		}
		echo "</table>";
	}
	//TiÌ€m tÃªn nhaÌ€ Ä‘Ã¢Ì€u tÆ° thÃ´ng qua ID
	public function findUserById($userid)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "SELECT * FROM user WHERE (UserID ='$userid')";
		if(!($result = mysql_query($sql))) die(mysql_error());
		$row = mysql_fetch_array($result);
		echo $row['Name'];
	}
}
?>